<template>
  <div :ref="className" :style="{ width: width, height: height }" />
</template>
<script>
// 引入 echarts
import * as echarts from 'echarts'
// 引入主题
require('echarts/theme/macarons')

const animationDuration = 3000
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  methods: {
    // 初始化函数
    initChart() {
      // 获取 dom对象并应用主题
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    // 设置配置
    setOptions() {
      const options = {
        legend: {
          show: true,
          type: 'scroll',
          data: ['Allocated Budget', 'Actual Spending', 'expected Spending'],
          bottom: 10
        },
        radar: {
          radius: '64%',
          center: ['50%', '42%'],
          // 雷达图分割的层数
          splitNumber: 8,
          // 雷达图绘制的样式----多边形
          shape: 'polygon',
          axisLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              width: 1,
              dashOffset: 0,
              color: 'rgba(238, 197, 102, 0.5)',
              cap: 'butt'
            }
          },
          axisName: {
            color: 'rgb(238, 197, 102)'
          },
          splitLine: {
            lineStyle: {
              color: [
                'rgba(238, 197, 102, 0.1)',
                'rgba(238, 197, 102, 0.2)',
                'rgba(238, 197, 102, 0.4)',
                'rgba(238, 197, 102, 0.6)',
                'rgba(238, 197, 102, 0.8)',
                'rgba(238, 197, 102, 1)'
              ].reverse()
            }
          },
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetY: 15,
              opacity: 0.8
            }
          },
          indicator: [
            { name: 'Sales', max: 6500 },
            { name: 'Administration', max: 16000 },
            { name: 'Information Technology', max: 30000 },
            { name: 'Customer Support', max: 38000 },
            { name: 'Development', max: 52000 },
            { name: 'Marketing', max: 25000 }
          ]
        },
        series: [
          {
            type: 'radar',
            symbolSize: 0,
            areaStyle: {},
            data: [
              {
                value: [4200, 3000, 20000, 35000, 50000, 18000],
                name: 'Allocated Budget'
              },
              {
                value: [5000, 14000, 28000, 26000, 42000, 21000],
                name: 'Actual Spending'
              },
              {
                value: [5001, 12000, 28000, 26000, 42000, 21000],
                name: 'expected Spending'
              }
            ],
            animationDuration: animationDuration
          }
        ]
      }
      this.chart.setOption(options)
    }
  }
}
</script>
